<?php


namespace app\api\logic;


use app\model\pms\GoodsBrowse;
use app\model\user\User;
use app\model\user\UserBalanceLog;
use app\model\user\UserIntegralLog;
use plugin\coupon\app\admin\logic\CouponReceiveLogLogic;
use support\constants\CacheKey;
use support\utils\CacheUtil;
use support\utils\Form;

class UserLogic extends \app\admin\user\logic\UserLogic
{
    public function __construct()
    {
        parent::__construct();

    }

    public static function getUserByCache($uid)
    {
        return CacheUtil::remember(CacheKey::LOGIN_KEY . $uid, function () use ($uid) {
            return User::where([
                'id' => $uid
            ])
                ->with([
                    'level',
                    'group',
                ])
                ->field([
                    "id",
                    "group_id",
                    "username",
                    "nickname",
                    "email",
                    "mobile",
                    "avatar",
                    "level_id",
                    "birthday",
                    "balance",
                    "score",
                ])
                ->find()
                ->toArray();
        }, 24 * 60 * 60);


    }

    public function getUserInfo($uid)
    {
        $data = $this->getUser($uid);
        $couponRe = new CouponReceiveLogLogic();
        $data['coupon'] = $couponRe->getCountByUserId($uid);
        return $data;
    }

    private function getUser($uid)
    {
        return $this->model->where([
            'id' => $uid
        ])
            ->with([
                'level',
                'group',
            ])
            ->field([
                "id",
                "group_id",
                "username",
                "nickname",
                "email",
                "mobile",
                "avatar",
                "level_id",
                "birthday",
                "balance",
                "score",
            ])
            ->find();

    }

    public function myBalance($uid)
    {
        $data = $this->getUser($uid);
        if ($data) {
            $data['wallet_activity'] = [
                [
                    'id' => 1,
                    'recharge' => 100,
                    'gift' => 10,
                ],
                [
                    'id' => 2,
                    'recharge' => 200,
                    'gift' => 20,
                ],
                [
                    'id' => 3,
                    'recharge' => 500,
                    'gift' => 50,
                ],
                [
                    'id' => 4,
                    'recharge' => 1000,
                    'gift' => 100,
                ],
            ];
        }
        return $data;
    }

    public function balanceLog($uid)
    {
        $data = $this->getUser($uid);
        if ($data) {
            $data['list'] = UserBalanceLog::where([
                'user_id' => $uid
            ])
                ->field([
                    'balance',
                    'create_time',
                    'remark',
                ])
                ->order("create_time desc")
                ->select();
        }
        return $data;
    }

    public function integralLog($uid)
    {
        $data = $this->getUser($uid);
        if ($data) {
            $data['list'] = UserIntegralLog::where([
                'user_id' => $uid
            ])
                ->field([
                    'integral',
                    'create_time',
                    'remark',
                ])
                ->order("create_time desc")
                ->select();
        }
        return $data;
    }

    public function getBrowser($uid)
    {
        $data = GoodsBrowse::alias("a")->where([
            'a.user_id' => $uid,
        ])
            ->join("goods g", "a.goods_id = g.id")
            ->field([
                'g.id',
                'g.name',
                'g.main_image',
            ])
            ->order('a.create_time desc')
            ->limit(6)
            ->select();

        return $data;
    }

    public function getBrowserPage($uid, Form $form)
    {
        $query = GoodsBrowse::alias("a")->where([
            'a.user_id' => $uid,
        ]);
        $query->join("goods g", "a.goods_id = g.id")
            ->field([
                'g.id',
                'g.name',
                'g.main_image',
                'g.min_price',
                'g.sales',
            ])
            ->order('a.create_time desc');
        $query->order($form->getSortField(), $form->getOrder());
        return $this->paginate($query, $form->getPage(), $form->getPageSize());
    }

    public function deleteBrowser($uid, $goods_id)
    {
        GoodsBrowse::where([
            'user_id' => $uid,
            'goods_id' => $goods_id,
        ])->delete();
    }

    public function clearBrowser($uid)
    {
        GoodsBrowse::where([
            'user_id' => $uid,
        ])->delete();
    }

}